﻿using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace PMIS.Shared.DTO
{
    public class UserDto : ModelBase, IDataErrorInfo
    {
        public string? Id { get; set; }

        [Required(ErrorMessage = "用户名不可为空")]
        public string UserName { get; set; }

        [Required(ErrorMessage = "姓名不可为空")]
        public string RealName { get; set; }

        public int? Gender { get; set; }

        public string? Birthday { get; set; }

        public string? Portrait { get; set; }

        [EmailAddress(ErrorMessage = "邮箱格式不正确")]
        public string? Email { get; set; }

        [StringLength(11, MinimumLength = 11, ErrorMessage = "请输入正确的手机号")]
        public string? Mobile { get; set; }

        public string? DepartmentId { get; set; }

        public string? DepartmentName { get; set; }

        public string? RoleId { get; set; }

        public int? Status { get; set; } = 1;

        public string? Error => null;

        public string? this[string columnName]
        {
            get
            {
                List<ValidationResult> validationResults = new List<ValidationResult>();

                bool result = Validator.TryValidateProperty(
                    GetType().GetProperty(columnName)?.GetValue(this),
                    new ValidationContext(this)
                    {
                        MemberName = columnName
                    },
                    validationResults);

                if (result)
                    return null;

                return validationResults.First().ErrorMessage;
            }
        }
    }
}
